package thread;
public class SyncDemo1 {
    static boolean success1 = false;
    static boolean success2 = false;
    public static void main(String[] args) throws InterruptedException {
        Bank bank = new Bank();
        int num = 0;
        while (true) {
            num++;
            Thread t1 = new Thread() {
                public void run() {
                    success1 = bank.getMoney(20000);//取钱
                }
            };
            Thread t2 = new Thread() {
                public void run() {
                    success2 = bank.getMoney(20000);//取钱
                }
            };
            t1.start();
            t2.start();
            Thread.sleep(10);
            if (success1 && success2) {
                System.out.println("卡BUG成功，两个人都取出来20000"+"，测试"+num+"次");
                break;
            } else {
             //  System.out.println("取款成功,只有一人取款成功");
                success1=false;
                success2=false;
                bank.saveAccount(20000);
            }
        }
    }
}
class Bank{
    private int account=20000;
    //同步
    public synchronized    boolean getMoney(int money){
        int account = getAccount();
        if (account>=money){
            account=account-money;
            Thread.yield();
            saveAccount(account);
            return true;
        }
        return false;
    }
void saveAccount(int account){
this.account=account;
}
private int getAccount(){
        return account;
}
}